Loop Statements execute its Body multiple times (while condition is TRUE or while iterating through Elements)
● for…in Statement iterates through Sequence or Array Elements and executes its Body for each Element.
● while Statement executes its Body while condition is TRUE. Condition is checked at the beginning of Body
● repeat...while Statement executes its Body while condition is TRUE. Condition is checked at the end of Body.
You can use
● break Jump Statement to exit Loop Statement
● continue Jump Statement to skip rest of the Body and continue with next iteration from the beginning of Body
for…in
//Iterate through Sequence Elements
for i in 1...5 {
print(i)
}
//Iterate through Array Elements
for person in ["John", "Lucy", "Bob"] {
print(person)
}
while
//Execute Body while i < 4. Check conditions at the begining of the Body.
var i = 0
while(i < 4) {
i = i + 1
print(i)
}
repeat ... while
//Execute Body while i < 4. Check conditions at the end of the Body.
var i = 0
repeat {
i = i + 1
print(i)
} while(i < 4)